package cn.wp.pet.controller;

import cn.wp.basic.util.AjaxResult;
import cn.wp.basic.util.PageList;
import cn.wp.pet.domain.Pet;
import cn.wp.pet.domain.PetType;
import cn.wp.pet.query.PetQuery;
import cn.wp.pet.service.IPetService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 宠物接口
 */
@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    /**
     * 分页高级查询列表显示
     * @param petQuery
     * @return
     */
    @PostMapping
    public PageList<Pet> queryPage(@RequestBody PetQuery petQuery){
        return petService.queryPage(petQuery);
    }

    @PostMapping("/list")
    public PageList<Pet> queryPageList(@RequestBody PetQuery petQuery){
        petQuery.setState(1);
        return petService.queryPage(petQuery);
    }

    /**
     * 新增和修改
     * @param department
     * @return
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Pet pet){
        try {
            if (pet.getId() == null){
                petService.add(pet);
            } else {
                petService.update(pet);
            }
            return new AjaxResult();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败！");
        }
    }

    /**
     * 批量上架
     * @param ids
     * @return
     */
    @PatchMapping("/onSale")
    public AjaxResult onSale(@RequestBody List<Long> ids){
        try {
            petService.onSale(ids);
            return new AjaxResult();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("上架失败！");
        }
    }

    /**
     * 批量下架
     * @param ids
     * @return
     */
    @PatchMapping("/offSale")
    public AjaxResult offSale(@RequestBody List<Long> ids){
        try {
            petService.offSale(ids);
            return new AjaxResult();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("下架失败！");
        }
    }

    /**
     * 删除一个对象
     * @param id
     * @return
     */
    @ApiOperation(value = "删除一个门店对象",notes = "根据id删除")
    @DeleteMapping("/{id}")
    public AjaxResult delect(@PathVariable("id") Long id){
        try {
            petService.delete(id);
            return new AjaxResult();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("删除失败！");
        }
    }

    /**
     * 宠物详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult getPetById(@PathVariable("id") Long id){
        Pet pet = petService.findOne(id);
        return AjaxResult.me().setResultObject(pet);
    }

}
